package jp.ddo.trismegistos.timer.adpter;

import java.text.DecimalFormat;
import java.util.List;

import jp.ddo.trismegistos.timer.R;
import jp.ddo.trismegistos.timer.entity.Time;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TimeAdpter extends ArrayAdapter<Time> {

	private List<Time> items;

	private LayoutInflater layoutInflater;

	private DecimalFormat df = new DecimalFormat();

	public TimeAdpter(final Context context, final int resourceId,
			final List<Time> items, final DecimalFormat df) {
		super(context, resourceId, items);
		this.items = items;
		this.layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.df = df;
	}

	@Override
	public View getView(final int position, final View convertView,
			final ViewGroup parent) {

		View v = convertView;
		if (v == null) {
			v = layoutInflater.inflate(R.layout.list, null);
		}

		final Time time = items.get(position);

		final ImageView imageView = (ImageView) v.findViewById(R.id.image);
		final TextView nowTimeView = (TextView) v.findViewById(R.id.nowTime);
		final TextView lapTimeView = (TextView) v.findViewById(R.id.lapTime);

		if (time.isTop()) {
			imageView.setImageResource(R.drawable.ranking11_1);
		} else {
			imageView.setImageBitmap(null);
		}

		nowTimeView.setText(df.format(time.getNowTime()));
		lapTimeView.setText(df.format(time.getLapTime()));

		return v;
	}
}
